package com.srpingde.hhh;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.springde.hhh.R;
import com.srpingde.hhh.application.HhhApp;
import com.srpingde.hhh.http.AsyncHttpResponseHandler;
import com.srpingde.hhh.jiekou.HHHIml;
import com.srpingde.hhh.jiekou.HHHInterface;

public class OpinionActivity extends Activity {
	ImageView back;
	HHHIml weiQuIml ;
	EditText opinion_edit;
	int kind = 0;
	String type = "商品问题";
	RadioGroup group;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_opinion);
		back = (ImageView) findViewById(R.id.back);
		group = (RadioGroup) findViewById(R.id.fenlei_group);
		layout();
		radioButtonLayout((RadioButton) findViewById(R.id.opinion_shangpin));
		radioButtonLayout((RadioButton) findViewById(R.id.opinion_peisong));
		radioButtonLayout((RadioButton) findViewById(R.id.opinion_jianyi));
		radioButtonLayout((RadioButton) findViewById(R.id.opinion_guzhang));
	}
	private void radioButtonLayout(RadioButton button){
		Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.fenlei_select);
		RadioGroup.LayoutParams layoutParams = (android.widget.RadioGroup.LayoutParams) button.getLayoutParams();
		layoutParams.width = (int) (HhhApp.getInstance().getDisplayHightAndWightPx()[1] * 0.245);
		layoutParams.height = bitmap.getHeight();
		button.setLayoutParams(layoutParams);
	}
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.opinion_shangpin:
			kind = 0;
			type = "商品问题";
			break;
		case R.id.opinion_peisong:
			kind = 1;
			type = "配送问题";
			break;
		case R.id.opinion_jianyi:
			kind = 2;
			type = "建议意见";
			break;
		case R.id.opinion_guzhang:
			kind = 3;
			type = "故障报错";
			break;
		case R.id.opinion_send_opinion:
			if(opinion_edit.getText().toString().trim().equals("")){
				HhhApp.getInstance().showDialog(OpinionActivity.this, "意见内容不能为空！");
			}else{
				sendOpinion();
			}
			break;
		}
	}
	//提交意见
	private void sendOpinion(){
		String url = HHHInterface.httpHeader + "api.php?act=suggestion&type="+kind+"&content="+opinion_edit.getText().toString().trim();
		weiQuIml.gengGaiMiMa(url,OpinionActivity.this,new AsyncHttpResponseHandler(){
			@Override
			public void onSuccess(String content) {
				super.onSuccess(content);
				try {
					JSONObject jsonObject = new JSONObject(content);
					if(jsonObject.getBoolean("return")){
						new AlertDialog.Builder(OpinionActivity.this).setTitle("提示").setMessage("反馈意见成功，我们会尽快处理您所反映的问题，感谢您对我们的支持").setPositiveButton("确定", new DialogInterface.OnClickListener() {
							
							@Override
							public void onClick(DialogInterface dialog, int which) {
								// TODO Auto-generated method stub
								finish();	
							}
						}).create().show();
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}
	private void layout() {
		weiQuIml = new HHHIml();
		opinion_edit = (EditText) findViewById(R.id.opinion_edit);
		// 头的高度
		TextView top_title_tv = (TextView) findViewById(R.id.top_title);
		RelativeLayout.LayoutParams titleLayoutParams = (android.widget.RelativeLayout.LayoutParams) top_title_tv
				.getLayoutParams();
		titleLayoutParams.height = HhhApp.getInstance().title_h();
		top_title_tv.setLayoutParams(titleLayoutParams);
		//
		RelativeLayout.LayoutParams backLayoutParams = (LayoutParams) back.getLayoutParams();
		backLayoutParams.height = HhhApp.getInstance().title_h();
		back.setLayoutParams(backLayoutParams);
	}
}
